# -*- coding: utf-8 -*-
"""
-------------------------------------------------
File Name： alpha_num_in_tow_rows_template
Description :
Author : 'li'
date： 2020/11/5
-------------------------------------------------
Change Activity:
2020/11/5:
-------------------------------------------------
"""
from ml.modules.container_num_recognition.single_container_recognition.module.check_result_template.container_type_processor import \
    ContainerTypeProcessor
from ml.modules.container_num_recognition.single_container_recognition.module.gen_res.container_num_check import \
    verification_container_num


class ThreeRowTemplate:
    def __init__(self, format_result_list):
        self.format_result_list = format_result_list
        self.company_number, self.serial_number, self.check_number = '', '', ''
        self.is_valid_container_number, self.container_type_number = False, ''
        self.company_num_start_line_index = -1

    def is_this_template(self):
        """
        is this template
        :return:
        """
        if len(self.format_result_list) < 4:
            return False
        for index, lst in enumerate(self.format_result_list):
            line_content = ''.join(lst)
            if len(line_content) == 4 and line_content.isalpha() and line_content[-1] == 'U':
                self.company_num_start_line_index = index
        if len(self.format_result_list) >= (self.company_num_start_line_index + 3):
            tmp_row_1 = ''.join(self.format_result_list[self.company_num_start_line_index + 1])
            tmp_row_2 = ''.join(self.format_result_list[self.company_num_start_line_index + 2])
            if len(tmp_row_1) == 6 and tmp_row_1.isdigit() and len(tmp_row_2) == 1 and \
                    tmp_row_2.isdigit():
                return True
        return False

    def get_result(self):
        """
        get result inference
        :return:
        """
        self.company_number = ''.join(self.format_result_list[self.company_num_start_line_index])
        self.serial_number = ''.join(self.format_result_list[self.company_num_start_line_index + 1])
        self.check_number = ''.join(self.format_result_list[self.company_num_start_line_index + 2])
        if verification_container_num(self.company_number, self.serial_number, self.check_number):
            self.is_valid_container_number = True
        for index, lst in enumerate(self.format_result_list):
            for item in lst:
                if len(item) == 4 and ContainerTypeProcessor(item).is_content_type():
                    self.container_type_number = item
